package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.AlipayPayLog;
import co.yixiang.modules.system.service.AlipayPayLogService;
import co.yixiang.modules.system.dto.queryCriteria.AlipayPayLogQueryCriteria;
import co.yixiang.modules.system.dto.AlipayPayLogDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author db
* @date 2020-10-16
*/
@AllArgsConstructor
@Api(tags = "支付宝支付日志管理")
@RestController
@RequestMapping("/api/alipayPayLog")
public class AlipayPayLogController {

    private final AlipayPayLogService alipayPayLogService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','alipayPayLog:list')")
    public void download(HttpServletResponse response, AlipayPayLogQueryCriteria criteria) throws IOException {
        alipayPayLogService.download(Convert.toList(AlipayPayLogDto.class, alipayPayLogService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询支付宝支付日志")
    @ApiOperation("查询支付宝支付日志")
    @PreAuthorize("@el.check('admin','alipayPayLog:list')")
    public ResponseEntity<Object> getAlipayPayLogs(AlipayPayLogQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(alipayPayLogService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增支付宝支付日志")
    @ApiOperation("新增支付宝支付日志")
    @PreAuthorize("@el.check('admin','alipayPayLog:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody AlipayPayLog resources){
        return new ResponseEntity<>(alipayPayLogService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改支付宝支付日志")
    @ApiOperation("修改支付宝支付日志")
    @PreAuthorize("@el.check('admin','alipayPayLog:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AlipayPayLog resources){
        alipayPayLogService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除支付宝支付日志")
    @ApiOperation("删除支付宝支付日志")
    @PreAuthorize("@el.check('admin','alipayPayLog:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        alipayPayLogService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}